package com.hera.base.web.vaadin.mantenimiento;


import static com.hera.base.util.InputData.getObjectData;
import static com.hera.base.util.InputData.getStringData;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.hera.base.util.FiltroDTO;
import com.hera.base.util.TableCustom;
import com.hera.base.web.BaseUI;
import com.hera.base.web.MantenimientoMaestroDetalle;
import com.hera.process.config.domain.LoginMethod;
import com.hera.process.config.domain.ParameterDetail;
import com.hera.process.config.rest.delegate.ConfigDelegate;
import com.vaadin.annotations.AutoGenerated;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.FieldEvents.TextChangeEvent;
import com.vaadin.event.FieldEvents.TextChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class LoginMethodUI extends BaseUI implements MantenimientoMaestroDetalle,ClickListener {

	/*- VaadinEditorProperties={"grid":"RegularGrid,20","showGrid":true,"snapToGrid":true,"snapToObject":true,"movingGuides":false,"snappingDistance":10} */

	@AutoGenerated
	private VerticalLayout mainLayout;
	@AutoGenerated
	private VerticalLayout verticalLayout_2;
	@AutoGenerated
	private HorizontalLayout lytMaestro;
	@AutoGenerated
	private VerticalLayout pnlFormulario;
	@AutoGenerated
	private HorizontalLayout hlytBotonera;
	@AutoGenerated
	private Button btnEliminar;
	@AutoGenerated
	private Button btnNuevo;
	@AutoGenerated
	private TextField txtUserSupervisor;
	@AutoGenerated
	private TextField txtBaseDN;
	@AutoGenerated
	private ComboBox cmbTipo;
	@AutoGenerated
	private TextField txtpuerto;
	@AutoGenerated
	private TextField txtHost;
	@AutoGenerated
	private TextField txtNombre;
	@AutoGenerated
	private Label lblTitulo;
	@AutoGenerated
	private HorizontalLayout lytTablaMaestra;
	@AutoGenerated
	private TableCustom tblfMaestro;
	private static final Logger log = Logger.getLogger(LoginMethodUI.class);
	
	private static final long serialVersionUID = 6354364049021506958L;
	
	@Autowired
	ConfigDelegate configdelegate;
	//para jugar con las variables si es null.
	boolean esNuevo;
	LoginMethod loginmethod;
	
	
	/**
	 * The constructor should first build the main layout, set the
	 * composition root and then do any custom initialization.
	 *
	 * The constructor will not be automatically regenerated by the
	 * visual editor.
	 */
	public LoginMethodUI() {
		buildMainLayout();
		setCompositionRoot(mainLayout);
	}
	
	@Override
	protected void postConstruct() {
		cargarFormulario();
		buscarMaestro();
		buscarDetalle();
	}
	
	@Override
	public void cargarFormulario() {
		
		txtNombre.setNullRepresentation("");
		txtHost.setNullRepresentation("");
		txtBaseDN.setNullRepresentation("");
		txtpuerto.setNullRepresentation("");
		txtUserSupervisor.setNullRepresentation("");
		 
		log.info("inicio cargarFormulario");
		esNuevo= true;
		loginmethod=null;
		txtNombre.addStyleName("fieldRequired");
		
		btnEliminar.setVisible(false);
		btnEliminar.addClickListener(this);
		btnNuevo.addClickListener(this);
		btnNuevo.setCaption("Nuevo");
		
		tblfMaestro.setTitulo("Maestro de Login Method");
		tblfMaestro.setHeight("200px");
		
		tblfMaestro.addColumn("name", "Nombre", 200, true,String.class);
		tblfMaestro.addColumn("serverName", "Host", 50, true,String.class);
		tblfMaestro.addColumn("serverPort", "Puerto", 50, true,String.class);
		tblfMaestro.addColumn("type.name", "Tipo", 180, true,String.class);
		//Llenado de Combo//////////////////////////
		BeanItemContainer<ParameterDetail> items = new BeanItemContainer<ParameterDetail>(ParameterDetail.class);
		items.addAll(configdelegate.findMetodosLogin());
		cmbTipo.setContainerDataSource(items);
		cmbTipo.setItemCaptionPropertyId("name");
		////////////////////////////////////////////
		tblfMaestro.construir();
		tblfMaestro.addTextChangeListener(new TextChangeListener() {
			private static final long serialVersionUID = 1L;
			@Override
			public void textChange(TextChangeEvent event) {
				buscarMaestro(event);
			}
		});
		
		tblfMaestro.addValueChangeListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
			loginmethod=(LoginMethod)tblfMaestro.getSelectedItem(event);
			esNuevo=(loginmethod==null);
			if(esNuevo){
				limpiarMaestro();
			}else{
				btnEliminar.setVisible(true);
				btnNuevo.setCaption("Modificar");
				txtNombre.setValue(loginmethod.getName());
				txtHost.setValue(loginmethod.getServerName());
				txtpuerto.setValue(loginmethod.getServerPort());
				cmbTipo.setValue(loginmethod.getType());
				txtBaseDN.setValue(loginmethod.getLdapBaseDn());
				txtUserSupervisor.setValue(loginmethod.getUserSupervisorDn());
			}
			}
		});
	}

	@Override
	public void buscarMaestro() {
		buscarMaestro(null);
	}

	@Override
	public void buscarMaestro(TextChangeEvent event) {
		log.info("inicio buscar maestro");
		FiltroDTO filtroDTO = tblfMaestro.getFiltroDTO(event);
		filtroDTO.addAlias("type");
		filtroDTO.addOrdenamientoAscendente("name");
		List<LoginMethod> lstLoginMethods=configdelegate.findAllLoginMethods(filtroDTO);
		tblfMaestro.setLstData(lstLoginMethods);
		limpiarMaestro();
		log.info("fin buscarMaestro");
	}

	@Override
	public void limpiarMaestro() {
		log.info("inicio limpiarMaestro");
		btnEliminar.setVisible(false);
		btnNuevo.setCaption("Nuevo");
		txtNombre.setValue("");
		txtHost.setValue("");
		txtpuerto.setValue("");
		cmbTipo.setValue(null);
		txtBaseDN.setValue("");
		txtUserSupervisor.setValue("");
		log.info("fin limpiarMaestro");
	}

	@Override
	public void eliminarMaestro() {
		log.info("inicio eliminarMaestro");
		configdelegate.deleteLoginMethod(loginmethod);
		buscarMaestro();
		limpiarMaestro();
		log.info("fin eliminarMaestro");
	}

	@Override
	public void guardarMaestro() {
		log.info("inicio GuardarMaestro");
		if(esNuevo){
		loginmethod= new LoginMethod();
		obtenerDatosFormulario(loginmethod);
		configdelegate.createLoginMethod(loginmethod);
		}else{
			obtenerDatosFormulario(loginmethod);
			configdelegate.updatedLoginMethod(loginmethod);
		}
		limpiarMaestro();
		buscarMaestro();
		log.info("fin guardarMaestro");
	}

	@Override
	public void buttonClick(ClickEvent event) {
		if(event.getButton().equals(btnEliminar)){
			eliminarMaestro();
		}else if(event.getButton().equals(btnNuevo)){
			guardarMaestro();
		}
	}

	private void obtenerDatosFormulario(LoginMethod loginMethod){
		loginMethod.setName(getStringData(txtNombre,"Nombre"));
		loginMethod.setServerName(getStringData(txtHost,"Host"));
		loginMethod.setServerPort(getStringData(txtpuerto,"Puerto"));
		loginMethod.setType(getObjectData(cmbTipo,ParameterDetail.class,"Tipo"));
		loginMethod.setLdapBaseDn(getStringData(txtBaseDN));
		loginMethod.setUserSupervisorDn(getStringData(txtUserSupervisor));
	}
	
	@Override
	public void buscarDetalle() {
		
		
	}

	@Override
	public void buscarDetalle(TextChangeEvent event) {
		
		
	}

	@Override
	public void limpiarDetalle() {
		
		
	}

	@Override
	public void eliminarDetalle() {
		
		
	}

	@Override
	public void guardarDetalle() {
		
		
	}


	@AutoGenerated
	private VerticalLayout buildMainLayout() {
		// common part: create layout
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(false);
		mainLayout.setWidth("100%");
		mainLayout.setHeight("100%");
		mainLayout.setMargin(false);
		mainLayout.setSpacing(true);
		
		// top-level component properties
		setWidth("100.0%");
		setHeight("100.0%");
		
		// verticalLayout_2
		verticalLayout_2 = buildVerticalLayout_2();
		mainLayout.addComponent(verticalLayout_2);
		
		return mainLayout;
	}

	@AutoGenerated
	private VerticalLayout buildVerticalLayout_2() {
		// common part: create layout
		verticalLayout_2 = new VerticalLayout();
		verticalLayout_2.setImmediate(false);
		verticalLayout_2.setWidth("-1px");
		verticalLayout_2.setHeight("-1px");
		verticalLayout_2.setMargin(false);
		verticalLayout_2.setSpacing(true);
		
		// lytMaestro
		lytMaestro = buildLytMaestro();
		verticalLayout_2.addComponent(lytMaestro);
		
		return verticalLayout_2;
	}

	@AutoGenerated
	private HorizontalLayout buildLytMaestro() {
		// common part: create layout
		lytMaestro = new HorizontalLayout();
		lytMaestro.setImmediate(false);
		lytMaestro.setWidth("-1px");
		lytMaestro.setHeight("-1px");
		lytMaestro.setMargin(false);
		lytMaestro.setSpacing(true);
		
		// lytTablaMaestra
		lytTablaMaestra = buildLytTablaMaestra();
		lytMaestro.addComponent(lytTablaMaestra);
		
		// pnlFormulario
		pnlFormulario = buildPnlFormulario();
		lytMaestro.addComponent(pnlFormulario);
		
		return lytMaestro;
	}

	@AutoGenerated
	private HorizontalLayout buildLytTablaMaestra() {
		// common part: create layout
		lytTablaMaestra = new HorizontalLayout();
		lytTablaMaestra.setImmediate(false);
		lytTablaMaestra.setWidth("580px");
		lytTablaMaestra.setHeight("-1px");
		lytTablaMaestra.setMargin(false);
		
		// tblfMaestro
		tblfMaestro = new TableCustom();
		tblfMaestro.setImmediate(false);
		tblfMaestro.setWidth("-1px");
		tblfMaestro.setHeight("-1px");
		lytTablaMaestra.addComponent(tblfMaestro);
		
		return lytTablaMaestra;
	}

	@AutoGenerated
	private VerticalLayout buildPnlFormulario() {
		// common part: create layout
		pnlFormulario = new VerticalLayout();
		pnlFormulario.setImmediate(false);
		pnlFormulario.setWidth("-1px");
		pnlFormulario.setHeight("-1px");
		pnlFormulario.setMargin(false);
		pnlFormulario.setSpacing(true);
		
		// lblTitulo
		lblTitulo = new Label();
		lblTitulo.setStyleName("titulo");
		lblTitulo.setImmediate(false);
		lblTitulo.setWidth("-1px");
		lblTitulo.setHeight("-1px");
		lblTitulo.setValue("Formulario de Registro");
		pnlFormulario.addComponent(lblTitulo);
		pnlFormulario.setComponentAlignment(lblTitulo, new Alignment(20));
		
		// txtNombre
		txtNombre = new TextField();
		txtNombre.setStyleName("fieldRequired");
		txtNombre.setImmediate(false);
		txtNombre.setDescription("Nombre");
		txtNombre.setWidth("200px");
		txtNombre.setHeight("-1px");
		txtNombre.setInputPrompt("Nombre");
		pnlFormulario.addComponent(txtNombre);
		
		// txtHost
		txtHost = new TextField();
		txtHost.setImmediate(false);
		txtHost.setDescription("Host");
		txtHost.setWidth("200px");
		txtHost.setHeight("-1px");
		txtHost.setInputPrompt("Host");
		pnlFormulario.addComponent(txtHost);
		
		// txtpuerto
		txtpuerto = new TextField();
		txtpuerto.setImmediate(false);
		txtpuerto.setDescription("Puerto");
		txtpuerto.setWidth("200px");
		txtpuerto.setHeight("-1px");
		txtpuerto.setInputPrompt("Puerto");
		pnlFormulario.addComponent(txtpuerto);
		
		// cmbTipo
		cmbTipo = new ComboBox();
		cmbTipo.setImmediate(false);
		cmbTipo.setDescription("Tipo");
		cmbTipo.setWidth("200px");
		cmbTipo.setHeight("-1px");
		pnlFormulario.addComponent(cmbTipo);
		
		// txtBaseDN
		txtBaseDN = new TextField();
		txtBaseDN.setImmediate(false);
		txtBaseDN.setDescription("Base DN");
		txtBaseDN.setWidth("200px");
		txtBaseDN.setHeight("-1px");
		txtBaseDN.setInputPrompt("Base DN");
		pnlFormulario.addComponent(txtBaseDN);
		
		// txtUserSupervisor
		txtUserSupervisor = new TextField();
		txtUserSupervisor.setImmediate(false);
		txtUserSupervisor.setDescription("User Supervisor");
		txtUserSupervisor.setWidth("200px");
		txtUserSupervisor.setHeight("-1px");
		txtUserSupervisor.setInputPrompt("User Supervisor");
		pnlFormulario.addComponent(txtUserSupervisor);
		
		// hlytBotonera
		hlytBotonera = buildHlytBotonera();
		pnlFormulario.addComponent(hlytBotonera);
		
		return pnlFormulario;
	}

	@AutoGenerated
	private HorizontalLayout buildHlytBotonera() {
		// common part: create layout
		hlytBotonera = new HorizontalLayout();
		hlytBotonera.setImmediate(false);
		hlytBotonera.setWidth("-1px");
		hlytBotonera.setHeight("-1px");
		hlytBotonera.setMargin(false);
		hlytBotonera.setSpacing(true);
		
		// btnNuevo
		btnNuevo = new Button();
		btnNuevo.setCaption("Nuevo");
		btnNuevo.setImmediate(true);
		btnNuevo.setWidth("-1px");
		btnNuevo.setHeight("-1px");
		hlytBotonera.addComponent(btnNuevo);
		
		// btnEliminar
		btnEliminar = new Button();
		btnEliminar.setCaption("Eliminar");
		btnEliminar.setImmediate(true);
		btnEliminar.setWidth("-1px");
		btnEliminar.setHeight("-1px");
		hlytBotonera.addComponent(btnEliminar);
		
		return hlytBotonera;
	}

	


}
